﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using System.Globalization;
using simulate_control.Helpers;

namespace simulate_control.Basic
{
    public partial class HighlightLabel : LabelX
    {
        public string LabelText { get; set; }
        public Color LabelSelectedColor { get; set; }
        public int LabelMaxLength { get; set; }
        public Font LabelSelectedFont{ get; set; }

        public HighlightLabel()
        {
            LabelText = "";
            LabelSelectedColor = Utils.ColorText;
            LabelSelectedFont = Utils.FontText;
        }

        /// <summary>
        /// An array of all positions in the text to be highlighted.
        /// </summary>
        public int[] HighlightPositions { get; set; }
        public Color HighlightColor { get; set; }

        // Paints the string and applies the highlighting style.
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            if (LabelText.Length > 0)
            {
                if (HighlightPositions == null)
                {
                    HighlightPositions = new int[] { };
                }

                var usedOffsets = new List<float>();

                for (var i = 0; i < LabelMaxLength; i++)
                {
                    var characterToPaint =
                        LabelText[i].ToString(CultureInfo.CurrentCulture);

                    var selectedFont = LabelSelectedFont;
                    var currentOffset = usedOffsets.Sum();
                    var offset = e.Graphics.MeasureString(characterToPaint,
                        selectedFont).Width - 2;
                    var SelectedColor = LabelSelectedColor;

                    if (HighlightPositions.Contains(i))
                    {
                        SelectedColor = HighlightColor;
                        e.Graphics.DrawRectangle(new Pen(Color.Yellow, 0), e.ClipRectangle.X + currentOffset,
                            e.ClipRectangle.Y, offset, e.ClipRectangle.Height);
                        e.Graphics.FillRectangle(new SolidBrush(Color.Yellow), e.ClipRectangle.X + currentOffset,
                            e.ClipRectangle.Y, offset, e.ClipRectangle.Height);
                    }

                    e.Graphics.DrawString(characterToPaint, selectedFont,
                        new SolidBrush(SelectedColor),
                        new RectangleF(e.ClipRectangle.X + currentOffset,
                            e.ClipRectangle.Y + 3, e.ClipRectangle.Width,
                            e.ClipRectangle.Height));

                    usedOffsets.Add(offset);
                }
            }
        }
    }
}
